﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace JYH.Business
{
    public class SyncData
    {

        /// <summary>
        /// 数据同步
        /// </summary>
        /// <param name="ClassName">同步文件类名(例如完整的类名JYH.Business.SmartERP)</param>
        /// <param name="MethodName">同步方法</param>
        /// <param name="args">同步的参数 第一个参数是日期(yyyy-MM-dd) 第二个参数是编号,第三的参数 Msg</param>
        /// <param name="Msg">反馈的信息</param>
        /// <returns></returns>
        public static bool SynchronousData(string ClassName, string MethodName, object[] args, out string Msg)
        {
            Msg=string.Empty;
            try
            {
                if (string.IsNullOrEmpty(ClassName)) 
                {
                    ClassName = SysParaInfoBLL.GetParaInfo("SyncClass", out Msg).ParaValue;
                }
                Type type = Type.GetType(ClassName)??Assembly.GetExecutingAssembly().GetType(ClassName);
                if (type == null) 
                {
                    Msg = "数据同步文件类不存在";
                    return false;
                }

                object instance = Activator.CreateInstance(type);
                MethodInfo method = type.GetMethod(MethodName, BindingFlags.Public | BindingFlags.Instance);
                if (method == null)
                {
                    Msg = "数据同步方法不存在";
                    return false;
                }
                object result = method.Invoke(instance, args);
                if (result == null) 
                {
                    Msg = "同步方法调用失败";
                    return false;
                }
                Msg = args[2].ToString();
                return (bool)result;
            }
            catch (Exception ex) 
            {
                Msg= ex.ToString();
                return false;
            }
        }


        /// <summary>
        /// ERP回抛
        /// </summary>
        /// <param name="ClassName">回抛文件类名(例如完整的类名JYH.Business.SmartERP)</param>
        /// <param name="MethodName">回抛方法</param>
        /// <param name="args">同步的参数 第一个参数是编号,第二的参数 Msg</param>
        /// <param name="Msg">反馈的信息</param>
        /// <returns></returns>
        public static bool BackToERP(string ClassName, string MethodName, object[] args, out string Msg)
        {
            Msg = string.Empty;
            try
            {
                if (string.IsNullOrEmpty(ClassName))
                {
                    ClassName = SysParaInfoBLL.GetParaInfo("SyncClass", out Msg).ParaValue;
                }
                Type type = Type.GetType(ClassName) ?? Assembly.GetExecutingAssembly().GetType(ClassName);
                if (type == null)
                {
                    Msg = "数据同步文件类不存在";
                    return false;
                }

                object instance = Activator.CreateInstance(type);
                MethodInfo method = type.GetMethod(MethodName, BindingFlags.Public | BindingFlags.Instance);
                if (method == null)
                {
                    Msg = "数据同步方法不存在";
                    return false;
                }
                object result = method.Invoke(instance, args);
                if (result == null)
                {
                    Msg = "同步方法调用失败";
                    return false;
                }
                Msg = args[1].ToString();
                return (bool)result;
            }
            catch (Exception ex)
            {
                Msg = ex.ToString();
                return false;
            }
        }

    }
}
